package com.dingding.order.core.pay.biz.pay.impl;

import java.math.BigDecimal;

import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.util.MD5Util;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.common.core.util.tools.wechat.WechatTool;
import com.dingding.common.core.util.tools.wechat.pay.WechatPayApi;
import com.dingding.common.core.util.tools.wechat.pay.WechatPayReqFactory;
import com.dingding.common.core.util.tools.wechat.pay.paybean.SecondSign;
import com.dingding.common.core.util.tools.wechat.pay.paybean.WechatPayOrderResponse;
import com.dingding.common.core.util.tools.wechat.pay.paybean.request.WechatPayOrderRequest;
import com.dingding.order.code.OrderPayCodeMsg;
import com.dingding.order.core.pay.biz.pay.IPaymentService;
import com.dingding.order.dto.pay.ReqOrderPayDTO;
import com.dingding.order.dto.pay.ReqTaxiOneLinePayDTO;
import com.dingding.order.dto.pay.ResTaxiOneLinePayDTO;

/**
 * 
 * @ClassName: WechatPaymentImpl 
 * @Description: 微信支付，生成预订单
 * @author CHEHAUNBO 
 * @date 2016年7月5日 上午10:30:12
 * @since V2.3.0
 * 
 */
@Component("wechatPayment")
@SuppressWarnings("unchecked")
public class WechatPaymentImpl implements IPaymentService {

	/**
	 * 
	 * 微信支付，生成预定单
	 * 
	 * 按照微信支付API调用规范，支付之前需要在微信支付平台生成预支付订单，此方法由此而来
	 * 
	 * URL参考：https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_4
	 * 
	 * @Title: payment
	 * @param object 支付客户端请求DTO
	 * @return boolean true 支付成功 false 支付失败
	 * @author CHEHAUNBO
	 * @since V2.3.0
	 * 
	 */
	@Override
	public <T> boolean payment(Object object, ResponseEntity<T> responseEntity) {

		// 订单ID
		String oid = null;
		// 支付用户ID
		BigDecimal thirdPartyPayMoney = new BigDecimal(0);
		// 客户端IP
		String ipAddress = null;
		if ((object instanceof ReqTaxiOneLinePayDTO)) {
			oid = ((ReqTaxiOneLinePayDTO) object).getOid();
			thirdPartyPayMoney = ((ReqTaxiOneLinePayDTO) object).getThirdPartyPayMoney();
			ipAddress = ((ReqTaxiOneLinePayDTO) object).getIpAddress();
		} else if ((object instanceof ReqOrderPayDTO)) {
			oid = ((ReqOrderPayDTO) object).getOid();
			thirdPartyPayMoney = ((ReqOrderPayDTO) object).getThirdPartyPayMoney();
			ipAddress = ((ReqOrderPayDTO) object).getIpAddress();
		}

		// 创建微信支付请求DTO
		WechatPayOrderRequest orderRequest = WechatPayReqFactory.createRequest(WechatPayOrderRequest.class, 2);
		// 初始化微信支付DTO数据 （微信金额单位：分）
		orderRequest.setWechatPayOrderRequest(oid, thirdPartyPayMoney.multiply(new BigDecimal(100)).intValue(), ipAddress);

		String apiSec = "j5axbIfMSZjsiEGAScGMCUhp5kO2Oqaj";// API安全秘钥
		String stringSignTemp = ToolUtil.lexSortBean(orderRequest, WechatPayOrderRequest.class) + "key=" + apiSec;
		String sign = MD5Util.md5Encrypt(stringSignTemp).toUpperCase();
		orderRequest.setSign(sign);

		// 调用微信API
		WechatPayApi api = new WechatPayApi();
		WechatPayOrderResponse orderResponse = api.placeAnOrder(orderRequest);

		// 调用微信支付API生成预订单失败
		if (orderResponse == null
				|| (orderResponse.getReturn_code().equals("FAIL") || orderResponse.getResult_code().equals("FAIL"))) {
			responseEntity.setCode(OrderPayCodeMsg.WEI_CHAT_PAY_FAIL.getCode());
			responseEntity.setErrorMessage(OrderPayCodeMsg.WEI_CHAT_PAY_FAIL.getMessage());
			responseEntity.setPromptMessage(OrderPayCodeMsg.WEI_CHAT_PAY_FAIL.getMessage());
			return false;
		}

		// 根据返回结果进行二次签名
		SecondSign secondSign = WechatTool.getSecondSign(orderResponse.getPrepay_id(), orderResponse.getNonce_str());

		ResTaxiOneLinePayDTO resTaxiOneLinePayDTO = new ResTaxiOneLinePayDTO(secondSign.getNoncestr(), oid,
				secondSign.getPrepayid(), secondSign.getSign(), thirdPartyPayMoney, secondSign.getTimestamp());

		responseEntity.setResponseBody((T) resTaxiOneLinePayDTO);

		return false;

	}

}
